const { fork } = require('child_process')
const { join } = require('path')
const Executor = require('../Executor')

class ProcessExecutor extends Executor {
    #process
    async onInit() {
        const modulePath = join(__dirname, 'ProcessExecutorLoder.js')
        const child = fork(modulePath, [this.logPath, this.logFile], {
            stdio: 'inherit'
        })
        child.on('message', this.on.bind(this))
        child.on('exit', this.close.bind(this))
        this.#process = child
        this.disposable.push(() => {
            child.kill('SIGINT')
        })
        this.logger?.info('创建进程成功, 进程: %s', child.pid)
    }
    async onSend(data) {
        this.#process.send(data)
    }
}

module.exports = ProcessExecutor
